$(function () {
    var table=document.getElementById("tbody-result");
    $.ajax({
        type:'GET',
        dataType:'json',
        url:contextPath+"all/course/"+theRequest.name,
        success:function (data) {
            var str="";
            for (var i=0;i<data.count;i++) {
                str += "<tr>" +
                    "<td>" + i + "</td>" +
                    "<td>" + data.list[i].name+"</td>"+
            //         "<td>" + data.list[i].phone+ "</td>" +
            //         "<td>" + data.list[i].sex + "</td>" +
            //         "<td>" + data.list[i].userid + "</td>" +
                    "<td><button onclick='save(this)'>保存</button><button onclick='update(this)'>修改</button><button onclick='del(this)'>删除</button></td>" +
                    "</tr>";
            }
            table.innerHTML=str;
        }
        ,
        error:function () {
            alert("查询失败")
        }
    })
});
function update(r) {
    var table=document.getElementById("tbody-result");
    var i=r.parentNode.parentNode.rowIndex-1;
    name=table.rows[i].cells[1].innerHTML;
    table.rows[i].cells[1].innerHTML="<input type='text' id='name' value=" + table.rows[i].cells[1].innerHTML+ " />";
}
function save(r) {
    var table=document.getElementById("tbody-result");
    var i=r.parentNode.parentNode.rowIndex-1;
    var ss=new Object();
    ss.name=document.getElementById("name").value;
    ss.course_dir=theRequest.name;
    $.ajax({
        type:'POST',
        url:contextPath+"new/course/"+name,
        async: false,
        contentType: "application/json; charset=utf-8",
        data:JSON.stringify(ss),
        dataType:"json",
        success:function (data) {
            alert(data.message)
            window.location.reload();
        },
        error:function () {
            alert("修改失败");
        }
    })
}
function del(r) {
    var table=document.getElementById("tbody-result");
    var ss=new Object();
    var i=r.parentNode.parentNode.rowIndex-1;
    ss.name=table.rows[i].cells[1].innerHTML;
    console.log(ss.name);
    $.ajax({
        type:'DELETE',
        url:contextPath+"delete/course/"+ss.name,
        success:function (data) {
            alert(data.message)
            window.location.reload();
        },
        error:function () {
            alert("删除失败");
        }
    })
}
$('#btn').on('click',function () {
    var table=document.getElementById("tbody-result");
    var ss=new Object();
    ss.name=document.getElementById("menuname").value;
    $.ajax({
        type:'POST',
        url:contextPath+"find/course/"+ss.name,
        success:function (data) {
            if(data.message=="存在"){
                var str="<tr>"+
                    "<td>"+0+"</td>"+
                    "<td>"+ss.name+"</td>"+
                    "<td><button onclick='save(this)'>保存</button><button onclick='update(this)'>修改</button><button onclick='del(this)'>删除</button></td>" +
                    "</tr>";
            }
            table.innerHTML=str;
        },
        error:function () {
            alert("查询失败");
        }
    })
});
$('#add').on('click',function () {
    var ss=new Object();
    ss.name=document.getElementById("menuname").value;
    ss.course_dir=theRequest.name;
    $.ajax({
        type:'POST',
        url:contextPath+"new/course",
        async: false,
        contentType: "application/json; charset=utf-8",
        data:JSON.stringify(ss),
        dataType:"json",
        success:function (data) {
            alert(data.message);
            window.location.reload();
        },
        error:function () {
            alert("添加失败");
        }
    })
});